import {
    BQGIS_video
} from './BQGIS-video'

import {
    globalLayer,
    globalTool
} from '../BQGIS-globalVar'

export function initPlane(viewer = window.viewer, callback = {}) {
    if (!globalLayer.videoPlaneLayer) {
        globalLayer.videoPlaneLayer = new mars3d.layer.GraphicLayer()
        viewer.addLayer(globalLayer.videoPlaneLayer)
    }
    if (!globalTool.videoPlaneDom) {
        globalTool.videoPlaneDom = new BQGIS_video()
    }
}

// 绘制幕布
export function drawPlane(url) {
    const id = getUUId()
    const videoElement = globalTool.videoPlaneDom.createVideoElement(id)
    if (!videoElement) return
    if (url) {
        globalTool.videoPlaneDom.setVideoSrc({
            src: url,
            videoElement
        })
    }
    // 构造投射体
    return globalLayer.videoPlaneLayer.startDraw({
        id,
        type: "plane",
        style: {
            heading: 0,
            pitch: 0,
            roll: 0,
            opacity: 1,
            material: videoElement,
            plane_normal: "x",
            dimensions_x: 80.0,
            dimensions_y: 45.0
        }
    })
}

// 加载视频幕布
export function addPlane(option) {
    const videoElement = globalTool.videoPlaneDom.createVideoElement(option.id)
    if (!videoElement) return
    globalTool.videoPlaneDom.setVideoSrc({
        src: option.url,
        videoElement
    })
    // 构造投射体
    const graphic = new mars3d.graphic.PlaneEntity({
        id: option.id,
        name: option.name,
        position: option.position,
        style: {
            heading: option.heading,
            pitch: option.pitch,
            roll: option.roll,
            opacity: option.opacity,
            color: "#fff",
            material: videoElement,
            plane_normal: "x",
            dimensions_x: 80.0,
            dimensions_y: 45.0
        },
    })
    globalLayer.videoPlaneLayer.addGraphic(graphic)
    return graphic
}

export function flyTo({
    position,
    heading,
    pitch,
    roll,
    viewer = window.viewer,
    complete = () => {}
}) {
    const bounding = new Cesium.BoundingSphere(mars3d.LngLatPoint.toCartesian(position), 100)
    viewer.camera.flyToBoundingSphere(bounding, {
        offset: new Cesium.HeadingPitchRange(viewer.camera.heading, viewer.camera.pitch, 100)
    })
}

export function removeVideoPlaneById(id, viewer = window.viewer) {
    const graphic = getGraphicById(id)
    if (!graphic) return
    globalTool.videoPlaneDom.removeVideoElement(id)
    globalLayer.videoPlaneLayer.removeGraphic(graphic)
}

export function removeVideoPlane(graphic, viewer = window.viewer) {
    if (!graphic) return
    globalTool.videoPlaneDom.removeVideoElement(graphic.id)
    globalLayer.videoPlaneLayer.removeGraphic(graphic)
}

export function changeUrl(graphic, url) {
    graphic.attr.url = url
    globalTool.videoPlaneDom.changeVideoSrc({
        src: url,
        videoElement: graphic.style.material
    })
}

export function getGraphicById(id) {
    if (globalLayer.videoPlaneLayer) {
        return globalLayer.videoPlaneLayer.getGraphicById(id)
    }
}

function getUUId() {
    let tem = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    let id = "",
        len = tem.length,
        i = 0
    for (i; i < len; i++) {
        if (tem[i] == "-") id += tem[i]
        else {
            let num = Math.floor(Math.random() * 16)
            id += num.toString(16)
        }
    }
    return id
}

export function getGraphics() {
    return globalLayer.videoPlaneLayer.getGraphics()
}

export function clear(viewer = window.viewer) {
    globalLayer.video3dLayer.clear(true)
    globalTool.videoPlaneDom.destroy()
}



export function bindEvent({
    onStart = () => {}, // 开始编辑
    onUpDate = () => {}, // 数据变更
    onEnd = () => {}, // 结束编辑
    onRemove = () => {}
}) {
    globalLayer.videoPlaneLayer.on('editStart', onStart) // 拖动修改位置
    globalLayer.videoPlaneLayer.on('editMouseMove', onUpDate) // 拖动修改位置
    globalLayer.videoPlaneLayer.on('editStyle', onUpDate) // 按轴平移，旋转
    globalLayer.videoPlaneLayer.on('editStop', onEnd) // 按轴平移，旋转
    globalLayer.videoPlaneLayer.on('removeGraphic', onRemove)
}